{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel('区分度检验数据.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q13的量表题数据\n",
    "group13A = data['13.以下作为您使用AI对话助手动机的符合程度—A.名声大噪']\n",
    "group13B = data['B.服务良好']\n",
    "group13C = data['C.学术专业']\n",
    "group13D = data['D.随便看看']\n",
    "group13E = data['E.娱乐休闲']\n",
    "group13F = data['F.猎奇心理']\n",
    "group13Name = ['A.名声大噪','B.服务良好','C.学术专业','D.随便看看','E.娱乐休闲','F.猎奇心理']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q14的量表题数据\n",
    "group14A = data['14.请您为下列AI对话助手的特征进行打分—A.全天候可用性']\n",
    "group14B = data['B.回答全面性']\n",
    "group14C = data['C.回答准确性']\n",
    "group14D = data['D.回答高效性']\n",
    "group14E = data['E.回答可理解性']\n",
    "group14F = data['F.回答所用知识的时效性']\n",
    "group14G = data['G.多语言性(能够接收和回答多种语言)']\n",
    "group14H = data['H.多端联结性(可多端同步使用)']\n",
    "group14I = data['I.多模态交互性(能够接收多种类型的文件)']\n",
    "group14J = data['J.个性化服务性']\n",
    "group14K = data['K.安全性隐私性']\n",
    "group14K.head()\n",
    "#创建excel文件\n",
    "writer = pd.ExcelWriter('信度检验结果.xlsx')\n",
    "group14Name = ['A.全天候可用性','B.回答全面性','C.回答准确性','D.回答高效性','E.回答可理解性','F.回答所用知识的时效性','G.多语言性(能够接收和回答多种语言)','H.多端联结性(可多端同步使用)','I.多模态交互性(能够接收多种类型的文件)','J.个性化服务性','K.安全性隐私性']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Q15的量表题数据\n",
    "group15A = data['15.请您为AI对话助手的功能或服务打分—A.了解该对话助手的各方面信息，如性能、特色等']\n",
    "group15B = data['B.专业智能体的使用或定制']\n",
    "group15C = data['C.下载对话助手的移动端、PC端']\n",
    "group15D = data['D.搜索与推理模型']\n",
    "group15E = data['E.聊天信息的共享']\n",
    "group15F = data['F.查看升级版的功能与价格']\n",
    "group15G = data['G.本地部署']\n",
    "group15Name = ['A.了解该对话助手的各方面信息，如性能、特色等','B.专业智能体的使用或定制','C.下载对话助手的移动端、PC端','D.搜索与推理模型','E.聊天信息的共享','F.查看升级版的功能与价格','G.本地部署']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q13_Cronbach alpha: 0.9142877181081827\n",
      "Q14_Cronbach alpha: 0.9288489958567546\n",
      "Q15_Cronbach alpha: 0.931910246145674\n"
     ]
    }
   ],
   "source": [
    "groups13 = [group13A,group13B, group13C, group13D, group13E, group13F]\n",
    "groups14 = [group14A, group14B, group14C, group14D, group14E, group14F, group14G, group14H, group14I, group14J, group14K]\n",
    "groups15 = [group15A, group15B, group15C, group15D, group15E, group15F, group15G]\n",
    "# 对groups整体进行Cronbach's alpha信度检验\n",
    "def cronbach_alpha(groups):\n",
    "    # 初始化一个列表来存储每个组的方差\n",
    "    itemvars = []\n",
    "    # 计算每个组的方差并添加到itemvars列表中\n",
    "    for group in groups:\n",
    "        itemvars.append(group.var())\n",
    "    # 计算总方差\n",
    "    totalvars = np.var(np.array(groups), axis=1).sum() + np.var(np.array(groups).sum(axis=0))\n",
    "    # 获取项目（组）的数量\n",
    "    number_of_items = len(groups)\n",
    "    # 计算Cronbach's alpha\n",
    "    return number_of_items / (number_of_items - 1) * (1 - np.sum(itemvars) / totalvars)\n",
    "\n",
    "alpha = cronbach_alpha(groups13)\n",
    "print('Q13_Cronbach alpha:', alpha)\n",
    "alpha = cronbach_alpha(groups14)\n",
    "print('Q14_Cronbach alpha:', alpha)\n",
    "alpha = cronbach_alpha(groups15)\n",
    "print('Q15_Cronbach alpha:', alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "corr: 0.6546918600761666\n",
      "Q13 Split-Half reliability: {'Spearman-Brown系数等长': np.float64(0.7913157438824057), 'Spearman-Brown系数不等长': np.float64(0.013085431651955137), 'Guttman Split-Half 系数': np.float64(0.9912763788986966)}\n",
      "corr: 0.8147716298662371\n",
      "Q14 Split-Half reliability: {'Spearman-Brown系数等长': np.float64(0.8979329591198119), 'Spearman-Brown系数不等长': np.float64(0.10167945012358459), 'Guttman Split-Half 系数': np.float64(0.9186564399011323)}\n",
      "corr: 0.7869097080192503\n",
      "Q15 Split-Half reliability: {'Spearman-Brown系数等长': np.float64(0.8807492672828133), 'Spearman-Brown系数不等长': np.float64(0.03857814704514073), 'Guttman Split-Half 系数': np.float64(0.9742812353032395)}\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import pearsonr\n",
    "import math\n",
    "\n",
    "# 再使用折半信度检验，并详细展示中间的计算过程与结果，对每一步添加详细的中文注释\n",
    "def split_half(groups):\n",
    "    # 将数据前后分为两半\n",
    "    first_half = np.array(groups)[:len(groups)//2]\n",
    "    second_half = np.array(groups)[len(groups)//2:]\n",
    "    \n",
    "    #计算两组数据的皮尔逊相关系数\n",
    "    corr, _ = pearsonr(first_half.sum(axis=0), second_half.sum(axis=0))\n",
    "    print('corr:', corr)\n",
    "    \n",
    "    # 计算前半部分和后半部分的Cronbach's α系数\n",
    "    alpha_first_half = cronbach_alpha(first_half)\n",
    "    alpha_second_half = cronbach_alpha(second_half)\n",
    "    \n",
    "    # 使用斯皮尔曼-布朗公式调整折半信度\n",
    "    split_half_reliability_equal_length = (2 * corr) / (1 + corr)\n",
    "    split_half_reliability_unequal_length = (len(first_half) / (len(first_half) - 1)) * (1 - (alpha_first_half + alpha_second_half) / 2)\n",
    "    \n",
    "    # 计算Guttman Split-Half 系数\n",
    "    guttman_split_half = (alpha_first_half + alpha_second_half) / 2\n",
    "    \n",
    "    return {\n",
    "        \"Spearman-Brown系数等长\": split_half_reliability_equal_length,\n",
    "        \"Spearman-Brown系数不等长\": split_half_reliability_unequal_length,\n",
    "        \"Guttman Split-Half 系数\": guttman_split_half\n",
    "    }\n",
    "\n",
    "result = split_half(groups13)\n",
    "print('Q13 Split-Half reliability:', result)\n",
    "result = split_half(groups14)\n",
    "print('Q14 Split-Half reliability:', result)\n",
    "result = split_half(groups15)\n",
    "print('Q15 Split-Half reliability:', result)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
